<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>使用游标获取所有文档</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="mongo.tutorial.counting.html">计算文档数量</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mongo.tutorial.criteria.html">设置查询条件</a></div>
 <div class="up"><a href="mongo.tutorial.html">教程</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="mongo.tutorial.cursor" class="section">
   <h2 class="title">使用游标获取所有文档</h2>
   <p class="para">
    要活的集合中的所有文档，我们需要
    <span class="function"><a href="mongocollection.find.html" class="function">MongoCollection::find()</a></span> 方法。
    find() 方法返回一个
    <a href="class.mongocursor.html" class="classname">MongoCursor</a>
    对象，允许我们遍历整个结果集合来读取文档。要查询所有的文档并显示它们，使用：

    <div class="example" id="mongo.tutorial.cursor-example">
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$connection&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoClient</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$collection&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$connection</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">database</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">collectionName</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$cursor&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$collection</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">find</span><span style="color: #007700">();<br />foreach&nbsp;(&nbsp;</span><span style="color: #0000BB">$cursor&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$id&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$value&nbsp;</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$id</span><span style="color: #DD0000">:&nbsp;"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$value&nbsp;</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

    </div>

    这样会输出整个集合中的101个文档。
    <em>$id</em> 是集合的 <em>_id</em> 字段（被转换成字符串），
    <em>$value</em> 是文档本身。
   </p>
   <div class="section" id="mongo.tutorial.cursor.seealso">
    <h2 class="title">参见</h2>
    <p class="para">
     <span class="function"><a href="mongocollection.find.html" class="function">MongoCollection::find()</a></span>
     方法的文档中有关于查询的详细信息。
    </p>
   </div>
  </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="mongo.tutorial.counting.html">计算文档数量</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mongo.tutorial.criteria.html">设置查询条件</a></div>
 <div class="up"><a href="mongo.tutorial.html">教程</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
